<template>
  <div>
    <div
      style="
      width: 500px;
      display: flex;
    "
    >
      <div style="flex: 1; border: 1px solid #333; padding: 5px; text-align: center;">
        上周使用人数
      </div>
      <div style="flex: 1; border: 1px solid #333; padding: 5px; text-align: center;">
        检查邮件次数
      </div>
      <div style="flex: 1; border: 1px solid #333; padding: 5px; text-align: center;">
        导出Word次数
      </div>
      <div style="flex: 1; border: 1px solid #333; padding: 5px; text-align: center;">
        发送邮件次数
      </div>
    </div>
    <div
      style="
      width: 500px;
      display: flex;
    "
    >
      <div style="flex: 1; border: 1px solid #333; padding: 5px; text-align: center;">
        {{ statistData.useNum }}
      </div>
      <div style="flex: 1; border: 1px solid #333; padding: 5px; text-align: center;">
        {{ statistData.checkMailLogsNum }}
      </div>
      <div style="flex: 1; border: 1px solid #333; padding: 5px; text-align: center;">
        {{ statistData.exportLogsNum }}
      </div>
      <div style="flex: 1; border: 1px solid #333; padding: 5px; text-align: center;">
        {{ statistData.sendMailLogsNum }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "using",
  data() {
    return {
      statistData: {
        useNum: 0,
        checkMailLogsNum: 0,
        exportLogsNum: 0,
        sendMailLogsNum: 0,
      },
    }
  },
  methods: {
    /* 获取上周的周一到周日的日期范围 */
    getWeekRangeDate() {
      let currentTime = new Date()
      let year = currentTime.getFullYear()
      let month = currentTime.getMonth()
      let date = currentTime.getDate()
      let day = (currentTime.getDay() || 7) + 7
      let currentMonthDayNums = new Date(year, month + 1, 0).getDate()

      let monday = new Date(year, month + 1, date - day + 1 - currentMonthDayNums)
      let sunday = new Date(year, month + 1, date - day + 7 - currentMonthDayNums)

      return [this.formatDate(monday, " 0:00:00 AM"), this.formatDate(sunday, " 11:59:59 PM")]
    },

    /* 格式化日期格式为 mm/dd/yyyy, hh:mm:ss */
    formatDate(date, suffix) {
      return `${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}, ${suffix}`
    },

    init() {
      const lastWeekRange = this.getWeekRangeDate()
      this.$axios
        .get("/getRecordData")
        .then(res => {
          let useNumMap = new Map()
          Object.values(res.data).forEach(t => {
            for (let [key, value] of Object.entries(t)) {
              if (Object.prototype.toString.call(value) === "[object Array]") {
                value.forEach(m => {
                  if (m.time > lastWeekRange[0] && m.time < lastWeekRange[1]) {
                    this.statistData[`${key}Num`]++
                    if (!useNumMap.has(t.userName)) {
                      useNumMap.set(t.userName, 1)
                    }
                  }
                })
              }
            }
          })
          this.statistData.useNum = useNumMap.size
          this.$forceUpdate()
        })
        .catch(err => {
          console.log(err)
        })
    },
  },
  mounted() {
    this.init()
  },
}
</script>

<style></style>
